import java.util.logging.Logger;

import card.AiQiYiCardService;
import com.alibaba.fastjson.JSON;
import coupon.CouponResult;
import coupon.CouponService;
import goods.DeliverReq;
import goods.GoodsService;

public class PrizeController 
{
  private String queryUserName(String uId) 
  {
    return ("花花");
  }
  private String queryUserPhoneNumber(String uId) 
  {
    return ("15200101232");
  }

  private Logger logger = Logger.getLogger(PrizeController.class.toString());
  public AwardRes awardToUser(AwardReq req) 
  {
    String reqJson = JSON.toJSONString(req);
    AwardRes awardRes = null;
    {
      try 
      {
        this.logger.info("奖品发放开始" + req.getuId() + "。req: " + reqJson);
        // 按照不同类型方法商品[1优惠券、2实物商品、3第三方兑换卡(爱奇艺)]
        if (req.getAwardType() == 1) 
        {
          CouponService couponService = new CouponService();
          CouponResult couponResult =
            couponService.sendCoupon(req.getuId(), req.getAwardNumber(), req.getBizId());
          if ("0000".equals(couponResult.getCode())) 
            awardRes = new AwardRes("0000", "发放成功");
          else 
            awardRes = new AwardRes("0001", couponResult.getInfo());
        } 
        else if (req.getAwardType() == 2) 
        {
          GoodsService goodsService = new GoodsService();
          DeliverReq deliverReq = new DeliverReq();
          {
            deliverReq.setUserName(this.queryUserName(req.getuId()));
            deliverReq.setUserPhone(this.queryUserPhoneNumber(req.getuId()));
            deliverReq.setSku(req.getAwardNumber());
            deliverReq.setOrderId(req.getBizId());
            deliverReq.setConsigneeUserName(req.getExtMap().get("consigneeUserName"));
            deliverReq.setConsigneeUserPhone(req.getExtMap().get("consigneeUserPhone"));
            deliverReq.setConsigneeUserAddress(req.getExtMap().get("consigneeUserAddress"));
          }
          
          Boolean isSuccess = goodsService.deliverGoods(deliverReq);
          awardRes = 
            isSuccess?  new AwardRes("0000", "发放成功") :
                        new AwardRes("0001", "发放失败");
        } 
        else if (req.getAwardType() == 3) 
        {
          String bindMobileNumber = queryUserPhoneNumber(req.getuId());
          AiQiYiCardService iQiYiCardService = new AiQiYiCardService();
          iQiYiCardService.grantToken(bindMobileNumber, req.getAwardNumber());
          awardRes = new AwardRes("0000", "发放成功");
        }
        this.logger.info("奖品发放完成" + req.getuId() + "。");
      } 
      catch (Exception e) 
      {
        this.logger.warning("奖品发放失败" + req.getuId() + "。req: " + reqJson + e);
        awardRes = new AwardRes("0001", e.getMessage());
      }
    }
    return(awardRes);
  }
}
